<?php

namespace App\Http\Controllers\Inventory;

use App\Entity\Inbound\InboundEntity;
use App\Http\Controllers\BaseController;
use App\Models\Grade\GradeModel;
use App\Models\Inventory\InboundModel;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class InventoryController extends BaseController
{
    public function index(Content $content)
    {
        $grid = Grid::make(new InboundModel(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('pmark_name', '牌号')->label();
            $grid->column('type', '业务类型')->display(function ($type) {
                return InboundEntity::getTypeName($type);
            });
            $grid->column('company_id', '公司')->display(function ($companyId) {
                $companyInfo = DB::connection('oa')->table('company')->where('id', $companyId)->first(['id', 'short_name']);
                return $companyInfo ? $companyInfo->short_name : '';
            });
            $grid->column('purchase_num', '合同号');
            $grid->column('realname', '采购人')->display(function () {
                $targetId = $this->target_id;
                $compactInfo = DB::connection('oa')->table('compact')->where('id', $targetId)->first(['id', 'compact_owner']);
                if (!empty($compactInfo)) {
                    $userInfo = DB::connection('oa')->table('user')->where('id', $compactInfo->compact_owner)->first(['id', 'realname']);
                    return $userInfo ? $userInfo->realname : '';
                }
                return '';
            });
            $grid->column('purchase_date', '采购日期')->display(function ($purchaseDate) {
                return date('Y-m-d', strtotime($purchaseDate));
            });
            $grid->column('purchase_id', '供应商')->display(function ($purchaseId) {
                $purchaseInfo = DB::connection('oa')->table('customer')->where('id', $purchaseId)->first(['id', 'name']);
                return $purchaseInfo ? $purchaseInfo->name : '';
            });

            $grid->column('qty', '数量');
            $grid->column('shipping_qty', '已出');
            $grid->column('last_qty', '现库');
            /*$grid->column('purchase_price', '单价');*/
            $grid->column('remarks', '备注');
            $grid->column('warehouse_id', '仓库')->display(function ($warehouseId) {
                $warehouseInfo = DB::connection('oa')->table('warehouse')->where('id', $warehouseId)->first();
                return $warehouseInfo ? $warehouseInfo->name : '';
            });

            $grid->disableToolbar();
            $grid->disableActions();
            $grid->disableRowSelector();
            $grid->fixColumns(1, -1);

            $grid->scrollbarX();
            $grid->simplePaginate();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('pmark_id', '牌号')->width(6)->select(function ($pmarkId) {
                    $grade = GradeModel::find($pmarkId);

                    if ($grade) {
                        return [$grade->id => $grade->name];
                    }
                })->ajax(url('qw/inventory/search'));
            });
        });
        return $content
            ->full()
            ->title('库存台账')
            ->body($grid);
    }

    public function search(Request $request)
    {
        $q = $request->get('q');

        return GradeModel::where('name', 'like', "%$q%")->paginate(null, ['id', 'name as text']);
    }
}
